package com.xdxc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdxc.mapper.BankQuestionMapper;
import com.xdxc.model.entity.BankQuestion;
import com.xdxc.response.Result;
import com.xdxc.service.BankQuestionService;
import com.xdxc.service.QuestionService;
import com.xdxc.vo.QuestionWithOptionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BankQuestionServiceImpl extends ServiceImpl<BankQuestionMapper, BankQuestion> implements BankQuestionService {

  @Autowired
    private QuestionService questionService;


  @Override
  public Result QuestionBank(Integer pageNum, Integer pageSize, Long bankId){
        Page<BankQuestion> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BankQuestion> wrapper = new QueryWrapper<>();

        if (bankId == null ) {
            return questionService.questionPage(pageNum, pageSize);
        }
        wrapper.eq("bank_id", bankId);

        List<BankQuestion> bankQuestionList=  this.page(page,wrapper).getRecords();

        List<Long> bankIdList= bankQuestionList.stream().map(BankQuestion::getQuestionId).collect(Collectors.toList());

        List<QuestionWithOptionsVo>  questionList= questionService.getQuestionBatchByIdListWithCorrectAnswer(bankIdList);

        Result result=      Result.success(questionList);
        result.setMsg("题目获取成功");
        result.add("totalPage",page.getTotal());
        result.add("currentPage",page.getCurrent());
        result.add("pageSize",page.getSize());


        return result;

    }

    @Override
    public Result BankQuestionPage(Integer pageNum, Integer pageSize, Long bankId) {
        Page<BankQuestion> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<BankQuestion> wrapper = new LambdaQueryWrapper<>();
        if (bankId == null ) {
            return questionService.questionPage(pageNum, pageSize);
        }


        wrapper.eq(BankQuestion::getBankId, bankId);

        // 执行带条件的分页查询
        Page<BankQuestion> resultPage = this.page(page, wrapper);



        Result result = Result.success(resultPage.getRecords())
                .setMsg("题目获取成功")
                .add("totalPage", resultPage.getTotal())
                .add("currentPage", resultPage.getCurrent())
                .add("pageSize", resultPage.getSize());

        return result;
    }



}